<?php

// No direct access
defined('_JEXEC') or die;
jimport( 'joomla.user.authentication' );

// Include the JLog class.
jimport('joomla.log.log');

/**
 * Utilities for manipulating data
 * 
 * @since		1.7
 */


class Utils
{
	
	
	
	/**
	 * Deletes HTML-Tags
	 * @param unknown_type $uncleaned
	 */
	public static function clearHTML($uncleaned)
	{
		$cleaned = preg_replace("/(<\/?)(\w+)([^>]*>)/e", " ", $uncleaned);
		return $cleaned;
	}


	public static function extractMedia($joomlacontent, &$media)
	{

		$realpath = "";
		$links = array();
		$suffix = "";
		if( preg_match_all("/.*?(<img .*?src=\"([^\s]*?)\" .*?>).*?/", $joomlacontent, $match)) {
			
			//unset($match[0]);
			
			
			foreach ($match[2] as $key => $path) {
				

				
				$suffix = strtolower(substr(strrchr($path,"."),1));
				$realpath = str_replace("/", DS, JPATH_ROOT.DS.$path); 
				$codedimg = "";
				
				if($img = @file_get_contents($realpath)) 
			   	{
			      $codedimg = base64_encode($img);
			   	}
				else{ //display error pic
			   		$suffix = "png";
			   		$codedimg = "iVBORw0KGgoAAAANSUhEUgAAAHgAAAAeCAYAAADnydqVAAAAKnRFWHRDcmVhdGlvbiBUaW1lAFNvIDEgSmFuIDIwMTIgMTM6NDk6NTIgKzAxMDCgK28AAAAAB3RJTUUH3AEBDDUnItW2mAAAAAlwSFlzAAAuIwAALiMBeKU/dgAAAARnQU1BAACxjwv8YQUAAArQSURBVHja7VoLUFTXGT4Li4K8EQU1gJrU8UlR4yNoMCpTquLExOqElJo4jS1GrbWZTjCOIw6ZpmNsNbbGVqvUR+Nr1KT1PSpWJyZFFBtKfYLio2IQ5M2y7qPft/dc9rKC7qXqZpr9Zz7Ye+7573/u+f7/P/85u0J4xSte+XZKHBDaDj1fYKinB++VR0s2cA9YJBTS3JU3gFpglKdfwCsPl62AXeK8cD8qz0udcuEl+RsrHYHbwkkw4U5Uvuyicx/4nVCco6OnX8orTpkvJEnJwmA3ukcyCbzMfl0AY0uiVbILgBGefrlvu0wVCpEOokqEn32t8HUlu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dZUArfXM8/UJeeVDail53fmpzwEVnnadfxiutC4lkxaimY27a3SnZxwgnuVyun/X0i3ilbeE3ODws1/X7KaH8yoO/u/L+8P3/VLi90esUXvGKV9qS/wLXiDX7hrOM3gAAAABJRU5ErkJggg==";
			   	}

			   	$newpath = "::img=".$path."::";
			   	
			   	if(!in_array($newpath, $media)){
			   		$media[$newpath]="<img src=\"data:image/".$suffix.";base64,".$codedimg." \"alt=\"Base64 encoded image: ".$path."\" />";;
			   	}
			   	
			   	
				$links[] = $newpath." ";
							
				
				//$links[$key] = $realpath;
			}
			
		}
		else {
			// keine verlinkten Medien gefunden
			//echo "keine Medien gefunden<br>";
		}
			
		$joomlacontent = str_replace($match[1], $links, $joomlacontent);
		
		return $joomlacontent;
	}
	
	
	public static function convertMedia($joomlacontent)
	{

		//$secure_JPATH_BASE = str_replace("\\", "/", JPATH_BASE);
		
		$realpath = "";
		$links = array();
		$suffix = "";
		if( preg_match_all("/.*(<img src=\"([^\s]*)\" .*>).*/", $joomlacontent, $match)) {
			
			//unset($match[0]);
			
			
			foreach ($match[2] as $key => $path) {
				
				error_log("Pfad: "+$path, 0);
				
				$suffix = strtolower(substr(strrchr($path,"."),1));
				
				$realpath = str_replace("/", DS, JPATH_ROOT.DS.$path); //_BASE
				$codedimg = "";
				if($img = @file_get_contents($realpath)) 
			   	{
			      $codedimg = base64_encode($img);
			   	}
				else{ //display error pic
			   		$suffix = "png";
			   		$codedimg = "iVBORw0KGgoAAAANSUhEUgAAAHgAAAAeCAYAAADnydqVAAAAKnRFWHRDcmVhdGlvbiBUaW1lAFNvIDEgSmFuIDIwMTIgMTM6NDk6NTIgKzAxMDCgK28AAAAAB3RJTUUH3AEBDDUnItW2mAAAAAlwSFlzAAAuIwAALiMBeKU/dgAAAARnQU1BAACxjwv8YQUAAArQSURBVHja7VoLUFTXGT4Li4K8EQU1gJrU8UlR4yNoMCpTquLExOqElJo4jS1GrbWZTjCOIw6ZpmNsNbbGVqvUR+Nr1KT1PSpWJy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cCJBEhkjTtuIR8Z5V4x8GIfIbxEfbYHqSx18Dl4EkR/F2hHFL0VRu4yy933ud+7+eCXzgJ0QO4AvgbRYsjl5HAP3QSzPH6yhc2PmT8Ksn3hTN6fCUcx5fq5Gie6Sv7OY4WXdrd0pH3fGQ7x+fTyhi1R6havXbZexLyjpw4exu4LB1AK+ta6fdueweg+YbmoXhSE/A4xujJsbUljMQvRNvEEmuEkgpdZUArfXM8/UJeeVDail53fmpzwEVnnadfxiutC4lkxaimY27a3SnZxwgnuVyun/X0i3ilbeE3ODws1/X7KaH8yoO/u/L+8P3/VLi90esUXvGKV9qS/wLXiDX7hrOM3gAAAABJRU5ErkJggg==";
			   	}
			   	//xmlrpc_set_type($codedimg, 'base64');
			   	
				$links[] = "<img src=\"data:image/".$suffix.";base64,".$codedimg." \"alt=\"Base64 encoded image: ".$path."\" />";
							
				
				//$links[$key] = $realpath;
			}
			
		}
		else {
			// keine verlinkten Medien gefunden
			//echo "keine Medien gefunden<br>";
		}
			
		$joomlacontent = str_replace($match[1], $links, $joomlacontent);
		
		return $joomlacontent;
	}
	
	public static function getMedia($joomlacontent)
	{

		//$secure_JPATH_BASE = str_replace("\\", "/", JPATH_BASE);
		
		$realpath = "";
		$links = array();
		
		if( preg_match_all("/.*<img src=\"([^\s]*)\" .*>.*/", $joomlacontent, $match)) {
			
			unset($match[0]);
			
			
			foreach ($match[1] as $key => $path) {
				
				$realpath = str_replace("/", "\\", JPATH_BASE."\\".$path);
				$codedimg = "";
				if($img = @file_get_contents($realpath)) 
			   	{
			      $codedimg = base64_encode($img);
			   	}
			   	xmlrpc_set_type($codedimg, 'base64');
				$links[$key] = $codedimg;
				
				//$links[$key] = $realpath;
			}
			
		}
		else {
			// keine verlinkten Medien gefunden
			//echo "keine Medien gefunden<br>";
		}
			
		
		return $links;
	}
	
		
	
	
	public static function getMediaTextOnly($joomlacontent)
	{

		$secure_JPATH_BASE = str_replace("\\", "/", JPATH_BASE);
		
		$links = array();
		if( preg_match_all("/.*<img src=\"([^\s]*)\" .*>.*/", $joomlacontent, $match)) {
			
			unset($match[0]);
			
			
			foreach ($match[1] as $key => $path) {
				$links[$key] = $secure_JPATH_BASE.$path;
			}
			
		}
		else {
			// keine verlinkten Medien gefunden
			//echo "keine Medien gefunden<br>";
		}
			
		
		return $links;
	}
	
	
	public static function deleteLog()
	{

		$fullpath = JPath::find(JPATH_ROOT  . DS . 'logs' . DS . 'anjolina', "com_anjolina.log.php");
		if($fullpath<>FALSE) {
			
			JFile::delete($fullpath);
			
		}
		else {
			//no log to delete
		}

	}
	
/** Prettifies an XML string into a human-readable and indented work of art
 *  @param string $xml The XML as a string
 *  @param boolean $html_output True if the output should be escaped (for use in HTML)
 * Posted by Eric (Google) on Thursday, November 20, 2008 
 * http://gdatatips.blogspot.de/2008/11/xml-php-pretty-printer.html
 */
static function xmlpp($xml, $html_output=false) {
    $xml_obj = new SimpleXMLElement($xml);
    $level = 4;
    $indent = 0; // current indentation level
    $pretty = array();
    
    // get an array containing each XML element
    $xml = explode("\n", preg_replace('/>\s*</', ">\n<", $xml_obj->asXML()));

    // shift off opening XML tag if present
    if (count($xml) && preg_match('/^<\?\s*xml/', $xml[0])) {
      $pretty[] = array_shift($xml);
    }

    foreach ($xml as $el) {
      if (preg_match('/^<([\w])+[^>\/]*>$/U', $el)) {
          // opening tag, increase indent
          $pretty[] = str_repeat(' ', $indent) . $el;
          $indent += $level;
      } else {
        if (preg_match('/^<\/.+>$/', $el)) {            
          $indent -= $level;  // closing tag, decrease indent
        }
        if ($indent < 0) {
          $indent += $level;
        }
        $pretty[] = str_repeat(' ', $indent) . $el;
      }
    }   
    $xml = implode("\n", $pretty);   
    return ($html_output) ? htmlentities($xml) : $xml;
}

}
